package com.zd.otherapi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.zd.escortaction.domain.EscortAction;
import com.zd.escortaction.dto.EscortActionDto;
import com.zd.escortaction.service.impl.EscortActionServiceImpl;
import com.zd.lwsq.domain.ApplicationInfo;
import com.zd.lwsq.service.ApplicationInfoService;
import com.zd.otherapi.service.ApiApplicationInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * des:
 *
 * @author fxiao
 * @date 2020/8/10 17:22
 */
@Service
public class ApiApplicationInfoSerImpl implements ApiApplicationInfoService {
    private final Logger log= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ApplicationInfoService applicationInfoService;
    @Autowired
    private EscortActionServiceImpl escortActionService;

    @Override
    public boolean checkRoleByIdCard(String roleKey, String idCardNo) {
        if(StringUtils.isEmpty(roleKey)){
            log.error("未传入需要比较的roleKey信息");
            return false;
        }
        SysUser sysUser=userMapper.selectUserByIdCard(idCardNo);
        if(sysUser==null){
            log.error("身份证号为{}的用户未注册帐号,系统查无此人",idCardNo);
            return false;
        }
        List<SysRole> roles = sysUser.getRoles();
        if(roles!=null){
            for (int i = 0; i < roles.size(); i++) {
                if(roleKey.equals(roles.get(i).getRoleKey())){
                    return true;
                }
            }
        }
        return false;
    }
    @Transactional
    @Override
    public AjaxResult changeLocation(EscortActionDto escortActionDto) {
        String roleKey="escort";
        if(!checkRoleByIdCard(roleKey,escortActionDto.getHsrySfzh())){
            log.error("检测到身份证号为{}的用户没有{}角色",escortActionDto.getHsrySfzh(),roleKey);
            return AjaxResult.error("您没有权限进行此次操作，请检查身份证号是否填写错误，如果确认无误，请联系管理员处理");
        }
        SysUser escort=userMapper.selectUserByIdCard(escortActionDto.getHsrySfzh());
        String escortId=escort.getUserId().toString();
        String message="";
        //如果信息正确
        if(escortActionDto.getQrxx()){
            ApplicationInfo applicationInfo=new ApplicationInfo();
            applicationInfo.setId(escortActionDto.getGlsqdxId());
            Date currentDate=new Date();
            switch (escortActionDto.getDz()){
                case "lkzs":
                    applicationInfo.setLkzssj(currentDate);
                    message="离开住所时间录入成功";
                    break;
                case "ddzt":
                    applicationInfo.setDdztsj(currentDate);
                    message="到达机场/火车站时间录入成功";
                    break;
            }
            applicationInfo.setBy7(escortId);
            applicationInfoService.updateApplicationInfo(applicationInfo);
        }else{
            //检查是否有错误类型信息
            String cwlx=escortActionDto.getCwlx().trim();
            if(StringUtils.isEmpty(cwlx)){
                log.error("确认信息为{}时，错误类型却为“{}”",escortActionDto.getQrxx(),cwlx);
                return AjaxResult.error("如果确认信息不正确，请告知错误类型");
            }
            message="已将此次错误信息保存";
        }
        //接下来将此次操作动作存储起来
        EscortAction escortAction=new EscortAction();
        BeanUtils.copyProperties(escortActionDto,escortAction);
        escortAction.setId(UUID.randomUUID().toString().replaceAll("-",""));
        escortAction.setHsryId(escortId);
        escortActionService.insertEscortAction(escortAction);
        return AjaxResult.success(message);
    }
}
